package com.lujianfei.componentdesign.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.RelativeLayout
import android.widget.TextView
import com.lujianfei.componentdesign.R

class CustomTab @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {

    var txt_tab_title: TextView?= null
    var title:String = ""

    init {
        val obtainStyledAttributes =
            context.obtainStyledAttributes(attrs, R.styleable.CustomTab)
        LayoutInflater.from(context).inflate(R.layout.widget_custom_tab, this, true)
        initView()
        initData(obtainStyledAttributes)
        obtainStyledAttributes.recycle()
    }

    private fun initData(obtainStyledAttributes: TypedArray) {
        txt_tab_title?.text = obtainStyledAttributes.getString(R.styleable.CustomTab_tabTitle)
    }

    private fun initView() {
        txt_tab_title = findViewById(R.id.txt_tab_title)
    }

    fun setTabTitle(title:String):CustomTab {
        this.title = title
        txt_tab_title?.text = title
        return this
    }

   fun setTabSelected(selected:Boolean):CustomTab {
       super.setSelected(selected)
       if (selected) {
           setBackgroundColor(0xFF2700B3.toInt())
           txt_tab_title?.setTextColor(0xfffbd008.toInt())
       } else {
           setBackgroundColor(0xFF3700B3.toInt())
           txt_tab_title?.setTextColor(0xffffffff.toInt())
       }
       return this
   }
}